package com.af.community.smart.business

import android.os.Bundle
import android.view.View
import com.af.community.smart.base.BaseActivity
import com.af.community.smart.base.getColorEx
import com.af.community.smart.base.getString
import com.af.community.smart.base.gone
import com.af.community.smart.model.CommonData
import com.af.community.smart.utils.*
import com.af.community.smart.viewmodel.TaskViewModel
import com.jeremyliao.liveeventbus.LiveEventBus
import kotlinx.android.synthetic.main.activity_task_detail.*
import org.jetbrains.anko.toast
import org.koin.androidx.viewmodel.ext.android.getViewModel

class TaskDetailActivity : BaseActivity() {

    private lateinit var mData: CommonData

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_task_detail)
        initTitle(title = "详情")
    }

    override fun getViewModel() = getViewModel<TaskViewModel>()

    override fun initTitle(title: String, name: String) {
        super.initTitle(title, name)
        mData = intent.getSerializableExtra("data") as CommonData

        task_grid.text = mData.gridName
        task_work.text = mData.tourType
        task_time.text = mData.tourDate
        task_memo.text = mData.remark
        task_name.text = mData.userName
        task_itime.text = mData.createDate.toFormatString()
        task_result.text = mData.comments
        if (mData.status == "1") {
            task_upcoming.gone()
            task_state.setTextColor(getColorEx(R.color.colorAccent))
            task_state.text = "已完成"
        } else {
            task_done.gone()
            task_state.setTextColor(getColorEx(R.color.orange))
            task_state.text = "待办理"
        }
    }

    override fun doClick(v: View) {
        super.doClick(v)
        when (v.id) {
            R.id.bt_iusse -> {
                if (task_input.text.isBlank()) {
                    toast("请输入办理结果")
                    return
                }

                getViewModel().getTaskData(
                    getString("userId"),
                    mData.id.orEmpty(),
                    task_input.trimString()
                )
                    .bindLifeCycle(getLifecycleOwner())
                    .subscribeByNoError(
                        event = getViewModel(),
                        onSuccess = {
                            toast("提交成功！")
                            LiveEventBus.get("task_done").post("done")
                            ActivityStack.screenManager.popActivities(this@TaskDetailActivity::class.java)
                        }
                    )
            }
        }
    }

}
